package com.cardbaobao.cardbabyclient.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.callback.OnBottomClickListener;

/**
 * 详情底部View
 * Created by zad
 * 2016/11/28 14:28
 */

public class DetailBottomView extends LinearLayout implements View.OnClickListener {

    private OnBottomClickListener onBottomClickListener;

    private LayoutInflater inflater;

    private View mContentView;

    private View mLLShare;

    private View mLLCollect;

    private View mLLOpe;

    private TextView mTvOpe;

    private ImageView mIvOpe;

    private ImageView mIvCollect;

    public DetailBottomView(Context context) {
        this(context, null);
    }

    public DetailBottomView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DetailBottomView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflater = LayoutInflater.from(context);
        mContentView = inflater.inflate(R.layout.layout_view_credit_detail_bottom, this);
        mLLShare = mContentView.findViewById(R.id.id_ll_share);
        mLLShare.setOnClickListener(this);
        mLLCollect = mContentView.findViewById(R.id.id_ll_collect);
        mLLCollect.setOnClickListener(this);
        mLLOpe = mContentView.findViewById(R.id.id_ll_apply);
        mLLOpe.setOnClickListener(this);
        mTvOpe = (TextView) mContentView.findViewById(R.id.id_tv_apply);
        mIvOpe = (ImageView) mContentView.findViewById(R.id.id_iv_apply);
    }

    /**
     * 详情底部申请按钮图标源
     *
     * @param imgResId
     */
    public void setBottomImgRes(int imgResId) {
        if (mIvOpe == null) {
            mIvOpe = (ImageView) findViewById(R.id.id_iv_apply);
        }
        if (mIvOpe != null) {
            mIvOpe.setImageResource(imgResId);
        }
    }

    /**
     * 详情底部申请按钮文本
     *
     * @param text
     */
    public void setBottomText(String text) {
        if (mTvOpe == null) {
            mTvOpe = (TextView) findViewById(R.id.id_tv_apply);
        }
        if (mTvOpe != null) {
            mTvOpe.setText(text);
        }
    }

    public void setOnBottomClickListener(OnBottomClickListener onBottomClickListener) {
        this.onBottomClickListener = onBottomClickListener;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.id_ll_share:
                if (onBottomClickListener != null) {
                    onBottomClickListener.onBottomShare();
                }
                break;
            case R.id.id_ll_collect:
                if (onBottomClickListener != null) {
                    onBottomClickListener.onBottomCollect();
                }
                break;
            case R.id.id_ll_apply:
                if (onBottomClickListener != null) {
                    onBottomClickListener.onBottomOpe();
                }
                break;
        }
    }

    /**
     * 设置收藏图片
     * @param resId
     */
    public void setCollectIcon(int resId) {
        if (mIvCollect == null) {
            mIvCollect = (ImageView) findViewById(R.id.id_iv_collect);
        }
        if (mIvCollect != null) mIvCollect.setImageResource(resId);
    }

    /**
     * 隐藏收藏功能
     */
    public void hideCollect(){
        mLLCollect.measure(0,0);
        LinearLayout.LayoutParams params = (LayoutParams) mLLOpe.getLayoutParams();
        params.weight = 1;
        params.height = mLLCollect.getMeasuredHeight();
        mLLCollect.setVisibility(GONE);
    }
}
